//
//  DPTestStatistics.m
//  UITestingKit
//
//  Created by Ofri Wolfus on 03/02/07.
//  Copyright 2007 Ofri Wolfus. All rights reserved.
//

#import "DPTestStatistics.h"
#import "DPTestsModule.h"
#import "DPTestStatistics.h"


@implementation DPTestStatistics

- (id)init {
	if ((self = [super init])) {
		_info = [[NSMutableDictionary allocWithZone:[self zone]] initWithCapacity:6U];
	}
	
	return self;
}

- (void)dealloc {
	[_info release]; _info = nil;
	[super dealloc];
}

- (DPTestsModule *)module {
	return [_info valueForKey:DPTestsModuleKey];
}

- (void)setModule:(DPTestsModule *)name {
	[_info setValue:name forKey:DPTestsModuleKey];
}

- (NSString *)caseName {
	return [_info valueForKey:DPTestCaseNameKey];
}

- (void)setCaseName:(NSString *)name {
	[_info setValue:name forKey:DPTestCaseNameKey];
}

- (NSString *)testName {
	return [_info valueForKey:DPTestNameKey];
}

- (void)setTestName:(NSString *)name {
	[_info setValue:name forKey:DPTestNameKey];
}

- (DPTestStatus)status {
	NSNumber *r = [_info valueForKey:DPTestResultKey];
	return r ? [r intValue] : DPTestPending;
}

- (void)setStatus:(DPTestStatus)val {
	[_info setValue:[NSNumber numberWithInt:val]
			 forKey:DPTestResultKey];
}

- (id)returnValue {
	return [_info valueForKey:DPReturnValueKey];
}

- (void)setReturnValue:(id)val {
	if (val)
		[_info setValue:val forKey:DPReturnValueKey];
}

- (NSTimeInterval)executionTime {
	NSNumber *t = [_info valueForKey:DPExecutionTimeKey];
	return t ? [t doubleValue] : 0.0;
}

- (void)setExecTime:(NSTimeInterval)t {
	[_info setValue:[NSNumber numberWithDouble:t]
			 forKey:DPExecutionTimeKey];
}

- (NSString *)failReason {
	return [_info valueForKey:DPFailReasonKey];
}

- (void)setFailReason:(NSString *)str {
	if (str)
		[_info setValue:str
				 forKey:DPFailReasonKey];
}

- (unsigned)numberOfIterations {
	NSNumber *n = [_info objectForKey:@"numberOfIterations"];
	return n ? [n unsignedIntValue] : 0U;
}

- (void)setNumberOfIterations:(unsigned)x {
	[_info setObject:[NSNumber numberWithUnsignedInt:x]
			  forKey:@"numberOfIterations"];
}

- (NSDictionary *)info {
	return _info;
}

- (NSString *)description {
	return [NSString stringWithFormat:@"DPTestStatistics <%p>: %@", self, [self info]];
}

- (NSString *)log {
	switch ([self status]) {
		case DPTestFailed:
			return [NSString stringWithFormat:@"Test '%@' of case '%@' failed after %f(sec) for reason: %@\n",
			   [self testName], [self caseName], [self executionTime], [self failReason]];
			
		case DPTestSucceeded:
			return [NSString stringWithFormat:@"Test '%@' of case '%@' passed successfully. Average execution time: %f(sec).\n",
				[self testName], [self caseName], [self executionTime]];
			
		case DPTestPending:
			return [NSString stringWithFormat:@"Test '%@' of case '%@' is pending for verification. Execution time is %f(sec).\n",
				[self testName], [self caseName], [self executionTime]];
	}
	
	return nil;
}

@end
